package com.mall.product.service.impl;

import com.mall.common.utils.Constant;
import com.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.product.dao.BrandDao;
import com.mall.product.entity.BrandEntity;
import com.mall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
       String key =  (String) params.get(Constant.KEY);
        QueryWrapper<BrandEntity> queryWrapper =   new QueryWrapper<BrandEntity>();
       if(StringUtils.isNotBlank(key)){
           queryWrapper.eq("brand_id" , key).or().like("name",key);
       }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void updateBrandRelation(BrandEntity brand) {
        this.updateById(brand);
        //TODO 更新品牌名称相关联的
        if(StringUtils.isNotBlank(brand.getName())){
            categoryBrandService.updateByBrandId(brand);
        }
    }

}